package com.lucifer.treasure.command;

import com.lucifer.treasure.enchant.effect.SwordAuraEnchantmentEffect;
import com.lucifer.treasure.enchant.effect.SwordAuraPvPSystem;
import com.lucifer.treasure.util.SwordAuraLogger;
import com.lucifer.treasure.util.SwordAuraRenderer;
import com.lucifer.treasure.config.SwordAuraConstants;
import com.lucifer.treasure.network.SwordAuraHUDPacket;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.List;

/**
 * 御剑术状态查询命令 - 为HUD提供准确的服务端数据
 */
public class SwordAuraStatusCommand {

    public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
        dispatcher.register(CommandManager.literal("sword_aura_status")
                .executes(SwordAuraStatusCommand::getStatus));
        
        System.out.println("🔧 [命令] 御剑术状态查询命令已注册：/sword_aura_status");
    }
    
    private static int getStatus(CommandContext<ServerCommandSource> context) {
        ServerCommandSource source = context.getSource();
        if (source.getEntity() instanceof ServerPlayerEntity player) {
            sendStatusInfo(player);
            return 1;
        }
        return 0;
    }

    private static void sendStatusInfo(ServerPlayerEntity player) {
        try {
            // 获取服务端的真实数据
            boolean isEnabled = SwordAuraEnchantmentEffect.isAuraEnabled(player.getUuid());
            
            // 检查御剑术附魔等级
            int auraLevel = SwordAuraEnchantmentEffect.getPlayerSwordAuraLevel(player);
            
            // 获取悬浮剑数据
            int visibleSwords = SwordAuraEnchantmentEffect.getVisibleFloatingSwordCount(player.getUuid());
            int hitEffectSwords = SwordAuraRenderer.getHitEffectSwordCount(player.getUuid());
            int recoveringSwords = SwordAuraRenderer.getRecoveringSwordCount(player.getUuid());
            int maxSwords = SwordAuraConstants.calculateSwordCount(auraLevel);
            
            // 获取敌对关系数据
            List<SwordAuraPvPSystem.HostilityInfo> hostilities = SwordAuraEnchantmentEffect.getPlayerHostilities(player);
            int hostilityCount = hostilities != null ? hostilities.size() : 0;
            
            // 🆕 使用自定义网络包发送数据（完全隐蔽，不会在聊天栏显示）
            SwordAuraHUDPacket.sendToClient(player, isEnabled, auraLevel, visibleSwords, 
                                          hitEffectSwords, recoveringSwords, maxSwords, hostilityCount);
            
            // 记录日志
            // SwordAuraLogger.log(SwordAuraLogger.LogLevel.INFO, SwordAuraLogger.Category.SWORD_MANAGEMENT,
            //     "🔧 [HUD状态] 玩家 " + player.getName().getString() + 
            //     " 请求状态 - 开启:" + isEnabled + " 等级:" + auraLevel + 
            //     " 可见剑:" + visibleSwords + "/" + maxSwords + 
            //     " 攻击剑:" + hitEffectSwords + " 恢复剑:" + recoveringSwords +
            //     " 敌对关系:" + hostilityCount);
                
        } catch (Exception e) {
            // SwordAuraLogger.log(SwordAuraLogger.LogLevel.ERROR, SwordAuraLogger.Category.SWORD_MANAGEMENT,
            //     "❌ [HUD状态] 获取状态失败: " + e.getMessage());
            SwordAuraHUDPacket.sendErrorToClient(player);
        }
    }
} 